<template>
  <div>
    <!-- data:表格的数据 -->
    <!-- default-expand-all:默认展开所以子菜单 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <!-- prop：数据的字段名 -->
      <!-- label：标签名 -->
      <!-- sortable：是否可以排序 -->
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="catename" label="分类名称" width="180"> </el-table-column>
      <el-table-column label="分类图片" width="120">
        <template slot-scope="scope">
          <img v-if="scope.row.img" :src="$host+scope.row.img" width="100">
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {getCatelistTree, cateDel} from '../../request'
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getList();
    // 通过$bus的$on方法，开启监听
    this.$bus.$on('update',()=> {
      this.getList();
    })
  },
  methods: {
    // 获取分类列表
    async getList() {
      let res = await getCatelistTree();
      this.tableData = res.list;
    },
    // 编辑角色
    edit(info) {
      this.$emit("edit", { ...info });
    },
    // 删除角色弹窗
    del(id) {
      // 弹窗，二次确认
      this.$confirm("确定删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮
        .then(() => {
          // 真的要删了
          this.delCate(id);
        })
        // 点击取消按钮
        .catch(() => {  
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 真的要删了
    async delCate(id) {
      let res = await cateDel(id);
      this.tableData = res.list;
    }
  },
};
</script>

<style>
</style>